<template>
    <div class="myAddress">
        <vheader :isLeft=true :title='title'/>
        <!-- 显示收货地址 -->
        <div class="address-view" v-if="this.allAddress">
            <div v-for="(item,index) in allAddress" :key=index>
            <div class="address-card"  @click="goSettlement(item)">
                <!-- 地址 -->
                <div v-if="allAddress">
                   <span class="tagClass">{{item.tag}}</span>
                   <span class="address-text">{{item.address}}</span>
                </div>
                <!-- 个人信息 -->
                <div class="address-card-body" >
                    <p class="address-card-title">
                        <span class="username">{{item.name}}</span>
                        <span class="gender" v-if="item.sex">{{item.sex}}</span>
                    </p>
                    <p class="address-card-address">
                        <span class="phone">{{item.phone}}</span>
                    </p>
                </div>
            </div>
        </div>
        </div>
        <!-- 底部新增地址 -->
        <div class="address-view-bottom" @click="$router.push('/addAddress')">
            <i class="fa fa-plus-circle"></i>
            <span>新增收货地址</span>
        </div>
    </div>
  </template>
<script>
    // 先新增地址往本地存储存，然后vuex从本地储存获取，我的收货界面从vuex拿
    import vheader from "../../components/vheader.vue";
    import axios from "axios";
    export default {
        name: 'myAddress',
        data() {
            return {
                title: "我的地址",
                allAddress: [],

            }
        },
        // beforeRouteEnter(to, from, next) {
        //     next(vm => vm.getUserAddress())
        // },
        created() {
            this.getUserAddress();
        },
        methods: {
            // 从接口请求地址
            getUserAddress() {
                let phone = this.$store.getters.userLogin;
                axios.post(`http://joeweiwx.xyz:8005/saveAddress:${phone}`).then(res => {
                    this.allAddress = res.data
                })
            },
            goSettlement(userAddress) {

                // 将要收货的地址存储到vuex
                this.$store.dispatch('setUserAddress', userAddress)
                this.$router.push('/settlement')
            }
        },
        components: {
            vheader
        },
    }
</script>
<style scoped>
    .myAddress {
        width: 100%;
        height: 100%;
        box-sizing: border-box;
        padding-top: 45px;
    }
    
    .address-view-bottom {
        position: fixed;
        height: 13.866667vw;
        bottom: 0;
        width: 100%;
        background: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
        border-top: 0.266667vw solid #ddd;
        color: #3190e8;
        font-size: 1rem;
    }
    
    .address-card {
        background-color: #fff;
        padding: 4vw;
        border-bottom: 1px solid #ddd;
        display: flex;
        min-height: 15.133333vw;
        overflow: hidden;
        flex-direction: column;
    }
    
    .tagClass {
        display: inline-block;
        position: relative;
        margin-right: 2.2vw;
        padding: 0.533333vw;
        color: #39DFCD;
        white-space: nowrap;
        border: 1px solid #98C8F4;
        border-radius: 0.133333vw;
        font-size: 0.6rem !important;
        line-height: 2.666667vw;
    }
    
    .address-text {
        line-height: 4.533333vw;
        font-weight: 900;
    }
    
    .address-card-body {
        margin-top: 0.7rem;
        font-size: 0.8rem;
    }
    
    .address-card-body p {
        display: inline-block
    }
    
    .username,
    .gender,
    .phone {
        color: #666666
    }
    
    .username {
        margin-right: 0.8rem;
    }
</style>